<!DOCTYPE html>
<html>
<head>
    <title>报告生成页面</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="/webjars/miniui/boot.js" type="text/javascript"></script>
    <script src="/static/js/common/common.js" type="text/javascript"></script>
    <link href="/webjars/miniui/css/demo.css" rel="stylesheet" type="text/css" />
    <link href="/static/css/default.css" rel="stylesheet" type="text/css" />
    <link href="/static/js/laydate/theme/default/laydate.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div style="padding: 15px 2px;">
    <tr>
        <td style="width: 20%">选择年份:</td>
        <td style="width: 80%">
            <input type="text" class="layui-input" id="year" placeholder="请选择...">
        </td>
        <td style="width: 20%">选择报告:</td>
        <td>
            <input name="tname" id="tname" class="mini-combobox" style="width: 200px"
                   textField="tname" valueField="id" emptyText="请选择..."
                   url="/azbj/sjtjfx/bgsc/getbgTemplateList?ttype=1" />
        </td>
        <td><a class="mini-button" id="btnBaogao" onclick="createbaogao()">生成报告</a></td>
        <td><a class="mini-button" style="float: right" plain="true" iconCls="icon-search" plain="true" onclick="searchFile()">查询</a></td>
        <td><input class="mini-textbox" id="filename" style="float: right" emptyText="请输入文件名称" onenter="onKeyEnter" /></td>
    </tr>

</div>

<div class="mini-fit">
    <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" ajaxType="get"
         sizeList="[20,50,100]" pageSize="50" allowResize="true" showLoading="true"
         showPager="true" idField="id" url="/azbj/sjtjfx/bgsc/getFilesInfoList?taskId=23">
        <div property="columns">
            <div name="yearDate" field="yearDate" align="center" headerAlign="center" allowSort="false" width="100">年份</div>
            <div name="fileName" field="fileName" align="center" headerAlign="center" allowSort="false" width="100">文件名称</div>
            <div name="uploadDate" field="uploadDate" align="center" headerAlign="center" allowSort="false">文件生成时间</div>
            <div name="username" field="username" align="center" headerAlign="center" allowSort="false">操作人</div>
            <div name="action" headerAlign="center" align="center" renderer="onActionRenderer" cellStyle="padding:0;"
                 allowSort="false" multiSelect="false">操作
            </div>
        </div>
    </div>
</div>

<script src="/static/js/laydate/laydate.js" type="text/javascript"></script>
<script type="text/javascript">

    function max() {
        var nowDate = new Date();
        return nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate();
    }

    function min() {
        var nowDate = new Date("2020-01-01T00:00:00");
        return nowDate.getFullYear() + "-" + (nowDate.getMonth() + 1) + "-" + nowDate.getDate();
    }

    //常规用法
    //年选择器
    laydate.render({
        elem: '#year',
        type: 'year',
        max: max(),
        min: min()
    });
    mini.parse();
    var grid = mini.get("datagrid1");
    grid.load();

    //根据文件名称查询
    function searchFile() {
        var filename = mini.get("filename").getValue();
        grid.load({filename: filename});
    }

    //查询
    function onKeyEnter(e) {
        searchFile();
    }

    //渲染列中按钮
    function onActionRenderer(e) {
        var s = '';
        s += '&nbsp;<a class="mini-button mini-button-success"   href="javascript:downloadFile()" >&nbsp;&nbsp;下载&nbsp;&nbsp;</a>&nbsp';
        s += '&nbsp;<a class="mini-button mini-button-danger"   href="javascript:delFile();" >&nbsp;&nbsp;删除&nbsp;&nbsp;</a>&nbsp';
        return s;
    }

    // 删除附件
    function delFile() {
        var row = grid.getSelected();
        if (row) {
            commonRemove("/azbj/sjtjfx/bgsc/delete/" + row.singleId, row, {
                _method: "delete",
                fileUrl: row.fileUrl
            }, callbackDelete);

            function callbackDelete(text) {
                if (text.code == 1) {
                    //mini.alert("操作成功！");
                } else {
                    mini.alert("操作失败！");
                }
                grid.reload();
            }
            grid.reload();
        } else {
            mini.alert("请选中一条记录");
        }
        grid.reload();
    }

    function callback() {
        grid.reload();
    }

    function downloadFile() {
        var row = grid.getSelected();
        if (row) {
            $.ajax({
                url: encodeURI("/azbj/sjtjfx/bgsc/ifFileExist?filepath=" + row.fileUrl),
                cache: false,
                success: function (result) {
                    if (result.code == 1) {
                        top.location.href = encodeURI("/azbj/sjtjfx/bgsc/downloadFile?fileUrl=" + row.fileUrl + "&fileName=" + row.fileName);
                    } else {
                        mini.alert("没有可下载的文件");
                    }
                }
            });
        } else {
            mini.alert("请选中一条记录");
        }
    }

    function createbaogao() {
        var year = $("#year").val();
        if (!year) {
            mini.alert("请选择年份");
            return;
        }
        var tid = mini.get("tname").getValue();
        if (!tid) {
            mini.alert("请选择报告");
            return;
        }
        grid.loading("操作中，请稍后......");
        $.ajax({
            url: encodeURI("/azbj/sjtjfx/bgsc/createbaogao?id=" + tid + "&year=" + year),
            cache: false,
            success: function (result) {
                if (result.code == 1) {
                    mini.alert("生成报告成功");
                } else {
                    mini.alert("生成报告失败");
                }
                grid.reload();
            },
            complete: function (result) {
                grid.reload();
            }
        });
    }
</script>
</body>
</html>